package com.itcrowd.ittao.view;

import com.itcrowd.ittao.R;
import com.itcrowd.ittao.service.ShoppingListManager;
import com.itcrowd.ittao.service.data.Price;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

public class MineFragment extends Fragment implements OnClickListener {

	private TextView mCostView;
	private TextView mIncomeView;
	
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		activity.getActionBar().setTitle(R.string.mine);
	}
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.mine_fragment, null, false);
		
		View order = view.findViewById(R.id.order);
		order.setOnClickListener(this);
		
		mCostView = (TextView) view.findViewById(R.id.cost);
		
		TextView incomeFormula = (TextView) view.findViewById(R.id.income_formula);
		String income_formula = getString(R.string.income_formula);
		income_formula = String.format(income_formula, String.valueOf(ShoppingListManager.INCOM_RATIO));
		incomeFormula.setText(income_formula);
		
		mIncomeView = (TextView) view.findViewById(R.id.income);
		updateInfo();
		return view;
	}

	@Override
	public void onResume() {
		super.onResume();
		updateInfo();
	}
	
	private void updateInfo() {
		mIncomeView.setText(ShoppingListManager.getInstance().getTotalIncomeString());
		String costStr = getString(R.string.cost_number);
		costStr = String.format(costStr, ShoppingListManager.getInstance().getTotalCostString());
		mCostView.setText(costStr);
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.order:
			startActivity(new Intent(getActivity(), OrderListActivity.class));
			break;
		}
		
	}
}
